package com.hawthorn.redis.service.sysconf.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.SysAppConfCacheMapper;
import com.hawthorn.redis.service.sysconf.SysAppConfCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 加载系统参数缓存服务实现类</p>
 * @author: andyten@tom.com | xukaixing@hotmail.com | xukaixing@hotmail.com
 * @created: 2021/11/11 8:48 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class SysAppConfCacheServiceImpl implements SysAppConfCacheService
{
  @Autowired
  RedisMyClient redisMyClient;

  @Resource
  private SysAppConfCacheMapper sysAppConfCacheMapper;

  /**
   * <p>todo 初始化方法</p>
   * @param:
   * @return: void
   * @author: andyten@tom.com | xukaixing@hotmail.com | xukaixing@hotmail.com
   * @date: 2021/11/11 8:48 PM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/11/11    andy.ten        v1.0.1             init
   */
  @Transactional(readOnly = true)
  public void init()
  {
    List<Map<String, Object>> sysAppConfCache = sysAppConfCacheMapper.selectAppConf();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_APPCONF_PREFIX, "*"));

    for (Map appConf : sysAppConfCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_APPCONF_PREFIX, appConf.get("para_key"));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_ID, MyUtil.getValue(appConf.get("id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_APPNAME, MyUtil.getValue(appConf.get("app_name")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_TYPE, MyUtil.getValue(appConf.get("type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAKEY, MyUtil.getValue(appConf.get("para_key")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARANAME, MyUtil.getValue(appConf.get("para_name")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAVAL1, MyUtil.getValue(appConf.get("para_val1")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAVAL2, MyUtil.getValue(appConf.get("para_val2")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAVAL3, MyUtil.getValue(appConf.get("para_val3")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAVAL4, MyUtil.getValue(appConf.get("para_val4")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_APPCONF_PARAVAL5, MyUtil.getValue(appConf.get("para_val5")));
    }
    log.info("====== load appconf to redis success ======");
  }
}
